package com.dede.rxbase.dialog;

import android.support.v4.app.Fragment;

import com.dede.rxbase.activity.BaseStateActivity;

import java.util.List;

/**
 * 略！略！略！皮一下很开心
 * Created by dede on 2018/2/28.
 */
public final class StateDialogHelper {

    private final BaseStateActivity activity;

    @StateDialog.State
    private int thisState;

    @StateDialog.State
    public int getState() {
        return thisState;
    }

    public StateDialogHelper(BaseStateActivity activity) {
        if (activity == null)
            throw new NullPointerException("BaseStateActivity is null");
        this.activity = activity;
    }

    public void showError() {
        thisState = StateDialog.ERROR;
        StateDialog errorDialog = activity.createErrorDialog();
        if (errorDialog != null) {
            errorDialog.setOnReloadListener(activity);
            errorDialog.show(activity);
        }
    }

    public void showLoading() {
        thisState = StateDialog.LOADING;
        StateDialog loadingDialog = activity.createLoadingDialog();
        if (loadingDialog != null) {
            loadingDialog.setOnReloadListener(activity);
            loadingDialog.show(activity);
        }
    }

    public void showEmpty() {
        thisState = StateDialog.EMPTY;
        StateDialog emptyDialog = activity.createEmptyDialog();
        if (emptyDialog != null) {
            emptyDialog.setOnReloadListener(activity);
            emptyDialog.show(activity);
        }
    }

    public void hideLoading() {
        List<Fragment> fragments = activity.getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof StateDialog) {
                ((StateDialog) fragment).dismiss();
            }
        }
    }
}
